<?php
/**
 * Description of page
 *
 * @author Henson
 */
class Page {
	
	public $defaultParameter = 'p';
	
	public $showPageCount = 3;

	public $total = 0;
	
	public $curPage = 0;
	
	public $pageUrl = '';
	
	public $pageRow = 0;
	
	public $totalPage = 0;
	
	public $preUrl = '';
	
	public $nexUrl ='';
	
	public $showPages = array();

	public function __construct($curlPage, $pageUrl, $total, $pageRow) 
	{
		$this->total	= $total;
		$this->curPage	= $curlPage;
		$this->pageRow	= $pageRow;
		$this->pageUrl	= $pageUrl;
	}
	
	public function run()
	{
		$this->totalPage = intval(ceil($this->total / $this->pageRow));
		if($this->curPage == 1)
		{
			$this->preUrl = '';
		}
		else
		{
			$this->preUrl = $this->pageUrl. '&'.$this->defaultParameter.'='.($this->curPage - 1);
		}
		if($this->curPage == $this->totalPage)
		{
			$this->nexUrl = '';	
		}
		else
		{
			$this->nexUrl = $this->pageUrl.'&'.$this->defaultParameter.'='.($this->curPage + 1);
		}
		
		if($this->showPageCount > 0)
		{
			if($this->totalPage <= $this->showPageCount)
			{
				for($i = 1; $i <= $this->totalPage; $i++)
				{
					if($i == $this->curPage)
					{
						$this->showPages["{$i}"] = 'curpage';
					}
					else
					{
						$this->showPages["{$i}"] = $this->pageUrl.'&'.$this->defaultParameter.'='.$i;
					}
				}
			}
			else
			{
				if($this->curPage <= 2)
				{
					for($i = 1; $i <= $this->showPageCount; $i++)
					{
						if($i == $this->curPage)
						{
							$this->showPages["{$i}"] = 'curpage';
						}
						else
						{
							$this->showPages["{$i}"] = $this->pageUrl.'&'.$this->defaultParameter.'='.$i;
						}
					}
				}
				elseif($this->curPage == $this->totalPage)
				{
					for($i = ($this->totalPage - 2); $i <= $this->totalPage; $i++)
					{
						if($i == $this->curPage)
						{
							$this->showPages["{$i}"] = 'curpage';
						}
						else
						{
							$this->showPages["{$i}"] = $this->pageUrl.'&'.$this->defaultParameter.'='.$i;
						}
					}
				}
				else
				{
					$this->showPages[($this->curPage - 1)] = $this->pageUrl.'&'.$this->defaultParameter.'='.($this->curPage - 1);
					$this->showPages[$this->curPage] = 'curpage';
					$this->showPages[$this->curPage + 1] = $this->pageUrl.'&'.$this->defaultParameter.'='.($this->curPage + 1);
				}
			}
		}
	}
}

?>
